<template>
	<view class="container">
		<view class="form">
			<u--form labelPosition="left" :model="userInfo" :rules="rules" labelWidth='auto' ref="uForm">
				<u-form-item label="巡检名称" prop="name" borderBottom>
					<u--input v-model="userInfo.name" border="none"></u--input>
				</u-form-item>
				<u-form-item label="巡检位置" prop="type" borderBottom>
					<u-radio-group v-model="userInfo.type" placement="row">
						<u-radio :customStyle="{marginRight: '8px'}" v-for="(item, index) in typeList" :key="index"
							:label="item.name" :name="item.value">
						</u-radio>
					</u-radio-group>
				</u-form-item>
				<u-form-item label="巡检时间" prop="name" borderBottom>
					<u--input v-model="userInfo.name" @focus="show=true" border="none"></u--input>
				</u-form-item>
				<u-form-item label="任务类型" prop="type" borderBottom>
					<u-radio-group v-model="userInfo.type" placement="row">
						<u-radio :customStyle="{marginRight: '8px'}" v-for="(item, index) in typeList" :key="index"
							:label="item.name" :name="item.value">
						</u-radio>
					</u-radio-group>
				</u-form-item>
			
				<u-form-item label="巡检人" prop="name" borderBottom>
					<u--input v-model="userInfo.name" @focus="personShow=true" border="none"></u--input>
				</u-form-item>
				<u-form-item label="内容" prop="content" borderBottom>
					<u--textarea v-model="userInfo.content" placeholder="请输入内容"></u--textarea>
				</u-form-item>
			
				<view class="btns">
					<u-button type="primary" @click="handleReset" :plain="true" text="重置"></u-button>
					<u-button type="primary" @click="handleSubmit" text="确定"></u-button>
				</view>
			</u--form>
		</view>

		<u-datetime-picker :show="show" @cancel='show=false' v-model="value" mode="datetime"></u-datetime-picker>
		<u-picker :show="personShow"  @cancel='personShow=false' :columns="columns"></u-picker>

	</view>
</template>

<script>
	export default {
		data() {
			return {
				show: false,
				personShow:false,
				columns: [
					['张三', '李四', '日本']
				],
				value: '',
				fileList: [],
				typeList: [{
						name: '留言',
						value: 1
					},
					{
						name: '建议',
						value: 2
					},
				],
				userInfo: {
					name: ''
				},
				rules: {
					'name': {
						type: 'string',
						required: true,
						message: '请填写标题',
						trigger: ['blur', 'change']
					},
				},
			};
		},
		onReady() {
			//如果需要兼容微信小程序，并且校验规则中含有方法等，只能通过setRules方法设置规则。
			this.$refs.uForm.setRules(this.rules)
		},
		methods: {
			handleReset() {
				this.$refs.uForm.resetFields()
				this.$refs.uForm.clearValidate()
			},
			handleSubmit() {
				this.$refs.uForm.validate().then(res => {
					uni.$u.toast('校验通过')
				}).catch(errors => {
					uni.$u.toast('校验失败')
				})
			},
			// 删除图片
			deletePic(event) {
				this[`fileList${event.name}`].splice(event.index, 1)
			},
			// 新增图片
			async afterRead(event) {
				// 当设置 multiple 为 true 时, file 为数组格式，否则为对象格式
				let lists = [].concat(event.file)
				let fileListLen = this[`fileList${event.name}`].length
				lists.map((item) => {
					this[`fileList${event.name}`].push({
						...item,
						status: 'uploading',
						message: '上传中'
					})
				})
				for (let i = 0; i < lists.length; i++) {
					const result = await this.uploadFilePromise(lists[i].url)
					let item = this[`fileList${event.name}`][fileListLen]
					this[`fileList${event.name}`].splice(fileListLen, 1, Object.assign(item, {
						status: 'success',
						message: '',
						url: result
					}))
					fileListLen++
				}
			},
			uploadFilePromise(url) {
				return new Promise((resolve, reject) => {
					let a = uni.uploadFile({
						url: 'http://192.168.2.21:7001/upload', // 仅为示例，非真实的接口地址
						filePath: url,
						name: 'file',
						formData: {
							user: 'test'
						},
						success: (res) => {
							setTimeout(() => {
								resolve(res.data.data)
							}, 1000)
						}
					});
				})
			},
		}
	}
</script>

<style lang="scss">
	.container {
		background-color: $uni-color-bg;
		min-height: 100vh;
		padding: 22upx 30upx;
		box-sizing: border-box;

		.btns {
			display: flex;

			button {
				margin: 0 30upx;
			}
		}
		.form{
			background-color: #fff;
			padding: 22upx 30upx;
			box-sizing: border-box;
		}
	}
</style>